package com.example.smartcanteen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.smartcanteen.mapper.AdminMapper;
import com.example.smartcanteen.mapper.GoodsMapper;
import com.example.smartcanteen.mapper.ShopMapper;
import com.example.smartcanteen.model.Admin;
import com.example.smartcanteen.model.Goods;
import com.example.smartcanteen.model.Shop;
import com.example.smartcanteen.utils.JwtUtil;
import com.example.smartcanteen.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    //管理员登录功能
    public ResponseMessage login(Admin admin) {
        ResponseMessage responseMessage = new ResponseMessage();

        //查看账号、密码是否一致
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", admin.getUsername())
                .eq("password", admin.getPassword());

        //返回 true 或 false
        Boolean success = adminMapper.selectOne(queryWrapper) != null;

//        //生成token
//        responseMessage.setToken(JwtUtil.createToken(admin.getUsername(),admin.getPassword()));

        responseMessage.setSuccess(success);
        return responseMessage;
    }


    //管理员审核店铺功能
    public ResponseMessage applyShop(int shopID, String state) {

        ResponseMessage responseMessage = new ResponseMessage();

        //根据商家ID查找指定商家、修改商家状态
        UpdateWrapper<Shop> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", shopID)
                .set("state", state);
        Boolean success = shopMapper.update(updateWrapper) != 0;
        responseMessage.setSuccess(success);
        return responseMessage;
    }


    //管理员审核商品功能
    public ResponseMessage applyGoods(int shopID,String goodsname, String newstate) {
        ResponseMessage responseMessage = new ResponseMessage();

        //根据商家ID、商品名称确定商品，修改商品状态
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("shopID", shopID)
                .eq("goodsname", goodsname)
                .set("state", newstate);
        Boolean success = goodsMapper.update(updateWrapper) != 0;
        responseMessage.setSuccess(success);
        return responseMessage;
    }
}
